package dict

import (
	"github.com/labstack/echo"
	resp "tacitus/library/response"
)

// 重新加载缓存
func ReloadCacheHandler(c echo.Context) error {
	return Reload()
}

func ShowKeyListHandler(c echo.Context) error {
	return resp.Data(c, keys)
}

func ShowItemListHandler(c echo.Context) error {
	i := new(Key)
	if err := c.Bind(i); err != nil {
		return err
	}
	data := make([]Item, 0)
	for _, item := range items[i.Key] {
		if item.IsAlias == 0 && item.Deprecated == 0 {
			data = append(data, item)
		}
	}
	return resp.Data(c, data)
}

func ShowItemValueNameMapHandler(c echo.Context) error {
	i := new(Key)
	if err := c.Bind(i); err != nil {
		return err
	}
	res := make(map[string]string)
	for _, item := range items[i.Key] {
		if item.IsAlias == 0 && item.Deprecated == 0 {
			res[item.Value] = item.Name
		}
	}
	return resp.Data(c, res)
}

func ShowItemNameValueMapHandler(c echo.Context) error {
	i := new(Key)
	if err := c.Bind(i); err != nil {
		return err
	}
	res := make(map[string]string)
	for _, item := range items[i.Key] {
		if item.Deprecated == 0 {
			res[item.Name] = item.Value
		}
	}
	return resp.Data(c, res)
}
